const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {
        Article_Name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        Brand_SerialNumber: [{ required: true, message: '请选择商品品牌', trigger: 'change' }],
        Category_SerialNumber: [{ required: true, message: '请选择商品类别', trigger: 'change' }],
        Series_SerialNumber: [{ required: true, message: '请选择商品系列', trigger: 'change' }],
        Item_SerialNumber: [{ required: true, message: '请选择商品款型', trigger: 'change' }],
        Unit_Price: [{ required: true, message: '请输入吊牌价', trigger: 'blur' }],
        Picture_URL: [{ required: true, message: '请输入图片地址', trigger: 'blur' }],
        App_ID: [{ required: true, message: '请输入App_ID', trigger: 'blur' }],
      },
      Specifications_Name: [{ required: true, message: '请输入规格', trigger: 'blur' }],
      Packing: [{ required: true, message: '请输入装箱数', trigger: 'blur' }],
      Unit_Price: [{ required: true, message: '请输入零售价', trigger: 'blur' }],
      brand: [],
      category: [],
      series: [],
      item: [],
      unit: [],
      supplier_info: [],
      fileList: [],
      fileList1: [],
      fileList2: [],
      fileList3: [],
      dialogImageUrl: '',
      dialogVisible: false,
      dialogVisible1: false,
      Specification: 0,
      form: {
        Article_SerialNumber: "",
        Article_Name: '',
        Brand_SerialNumber: '',
        Category_SerialNumber: '',
        Series_SerialNumber: '',
        Item_SerialNumber: '',
        Size: '',
        Unit_Price: '',
        Picture_URL: '',
        Weight: '',
        Weight_Unit: '',
        Package_Quantity: '',
        SKU_Package_Size: '',
        Carton_Quantity: '',
        App_ID: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        pic: '',
        Release_Type: '0',
        Children: {
          article_pic: [],
          article_barcode: [],
          article_specifications: [
            {
              Article_Specifications_SerialNumber: '',
              Article_SerialNumber: '',
              Net_Content: '',  //净含量
              Unit_Price: '', //零售价
              Packing: '', //装箱数
              PackingSize: '', //装箱尺寸
              OutsidePackingSize: '', //外箱尺寸
              Specifications_Name: '', //规格说明
              Picture_URL: '',   //规格图片
              pic: ''
            }
          ]
        }
      },
      loading: false
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      let _this = this;
      // 获取品牌
      $.ajax({
        url: DomainName + '/ShopBrand',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          _this.brand = res
          // console.log(res)
          // for (let i = 0; i < res.length; i++) {
          //   _this.brand.push(res[i].Brand_Name);
          // }
        }
      })
      $.ajax({
        url: DomainName + '/GoodsFileInquiry',
        type: 'POST',
        data: { Data: "supplier_info" },
        success: function (res) {
          for (let i = 0; i < res.length; i++) {
            _this.supplier_info.push(res[i]);
          }
          console.log(res)
        }
      })
      // SKU单位
      $.ajax({
        url: DomainName + '/ArticleUnit',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          _this.unit = res
          console.log(_this.unit)
          // for (let i = 0; i < res.length; i++) {
          //   _this.unit.push(res[i].Unit);
          // }
        }
      })
      // 商品类别
      $.ajax({
        url: DomainName + '/ShopCategory',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          _this.category = res
          console.log(_this.category)
          // for (let i = 0; i < res.length; i++) {
          //   _this.category.push(res[i].Category_Name);
          // }
        }
      })
      //商品系列
      $.ajax({
        url: DomainName + '/ShopSeries',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          _this.series = res
          console.log(_this.series)
          // for (let i = 0; i < res.length; i++) {
          //   _this.series.push(res[i].Series_Name);
          // }
        }
      })
      //商品款型
      $.ajax({
        url: DomainName + '/ShopItem',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          _this.item = res
          console.log(_this.item)
          // for (let i = 0; i < res.length; i++) {
          //   _this.item.push(res[i].Item_Name);
          // }
        }
      })
    },
    // 提交表单
    submitForm(formName) {
      var reg = /^\d*\.{0,1}\d{0,1}$/
      let _this = this
      //验证子表数据
      if (!reg.test(this.form.Unit_Price)) {
        return _this.$message.error("吊牌价输入错误")
      }
      console.log('表单信息', _this.form.Children)
      if (_this.form.Children.article_specifications[0].Specifications_Name == '') {
        return _this.$message.error("请输入规格说明")
      }
      if (_this.form.Children.article_specifications[0].Packing == '') {
        return _this.$message.error("请输入SKU数量")
      }
      // if ( _this.form.Children.article_specifications[0].PackingSize == '' ) {
      //   return _this.$message.error("请输入装箱尺寸")
      // }
      // if (_this.form.Children.article_specifications[0].OutsidePackingSize == '') {
      //   return _this.$message.error("请输入外箱尺寸")
      // }
      if (_this.form.Children.article_specifications[0].Unit_Price == '') {
        return _this.$message.error("请输入零售价")
      }
      if (_this.form.Children.article_specifications[0].Net_Content == '') {
        return _this.$message.error("请输入净含量")
      }
      //限制只能输入一个小数点
      var a = _this.form.Children.article_specifications[0].Unit_Price
      if (!reg.test(a)) {
        return _this.$message.error("输入的零售价有误")
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          _this.fileList.forEach(function (item, index) {
            _this.form.Children.article_pic.push({
              article_picNumber: '',
              Article_SerialNumber: '',
              Picture_Order: item.Picture_Order,
              Picture_Type: item.Picture_Type,
              Picture_Url: item.Picture_Url,
            });
          })
          _this.fileList1.forEach(function (item, index) {
            _this.form.Children.article_pic.push({
              article_picNumber: '',
              Article_SerialNumber: '',
              Picture_Order: item.Picture_Order,
              Picture_Type: item.Picture_Type,
              Picture_Url: item.Picture_Url,
            });
          })
          _this.fileList2.forEach(function (item, index) {
            _this.form.Children.article_barcode.push({
              BarCode: '',
              Article_SerialNumber: '',
              Picture_Order: item.Picture_Order,
              Picture_Url: item.Picture_Url,
            });
          })
          _this.form.Children.article_specifications.forEach(item => {
            item.pic = ''
          })
          console.log(_this.form)
          $.ajax({
            url: DomainName + '/getAddAction',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                article_info: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("操作成功")
                window.location.href = './Goods.html'
              } else {
                _this.$message.error("操作失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    add_article_specifications() {
      this.form.Children.article_specifications.push({
        Article_Specifications_SerialNumber: '',
        Article_SerialNumber: '',
        Net_Content: '',  //净含量
        Unit_Price: '', //零售价
        Packing: '', //装箱数
        PackingSize: '', //装箱尺寸
        OutsidePackingSize: '', //外箱尺寸
        Specifications_Name: '', //规格说明
        Picture_URL: '',
        pic: ''
      })
    },
    remove_article_specifications() {
      if (this.form.Children.article_specifications.length > 1) {
        this.form.Children.article_specifications.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    handleSpecification(index) {
      this.Specification = index
      console.log(this.Specification);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    onExcel() {
      this.dialogVisible1 = true
    },
    // 商品规格图片
    Specifications(file) {
      let _this = this
      let num = 1
      if (file.size > 150000 && file.size < 500000) {
        num = 0.4
      } else if (file.size > 500000) {
        num = 0.2
      }
      console.log(file);
      new Compressor(file, {
        quality: num,
        success(file) {
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                if (Result.Status == "success") {
                  _this.form.Children.article_specifications[_this.Specification].pic = e.target.result
                  _this.form.Children.article_specifications[_this.Specification].Picture_URL = Result.Picture_Url
                  _this.$message.success('上传成功')
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //商品封面图片
    uploadFront(file) {
      let _this = this
      let num = 1
      if (file.size > 150000 && file.size < 500000) {
        num = 0.4
      } else if (file.size > 500000) {
        num = 0.2
      }
      new Compressor(file, {
        quality: num,
        // if(file.size) {
        // }
        success(file) {
          console.log(file.size)
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                _this.form.pic = e.target.result
                if (Result.Status == "success") {
                  _this.form.Picture_URL = Result.Picture_Url
                  _this.$message.success('上传成功')
                  console.log(Result.Picture_Url);
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //商品条码
    uploadBarcode(file) {
      let _this = this
      this.fileList2.push({
        BarCode: '',
        Article_SerialNumber: '',
        Picture_Order: this.fileList.length + 1,
        Picture_Url: '',
        pic: ''
      })
      let num = 1
      if (file.size > 150000 && file.size < 500000) {
        num = 0.4
      } else if (file.size > 500000) {
        num = 0.2
      }
      new Compressor(file, {
        quality: num,
        success(file) {
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            _this.fileList2[_this.fileList2.length - 1].pic = e.target.result
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                if (Result.Status == "success") {
                  _this.fileList2[_this.fileList2.length - 1].Picture_Url = Result.Picture_Url
                  _this.$message.success('上传成功')
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //商品主图轮播图片
    uploadBanner(file) {
      let _this = this
      this.fileList.push({
        article_picNumber: '',
        Article_SerialNumber: '',
        Picture_Order: this.fileList.length + 1,
        Picture_Type: '0',
        Picture_Url: '',
        pic: ''
      })
      let num = 1
      if (file.size > 150000 && file.size < 500000) {
        num = 0.4
      } else if (file.size > 500000) {
        num = 0.2
      }
      new Compressor(file, {
        quality: num,
        success(file) {
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                  _this.$message(Result.Picture_Url)
                }
              },
              success: function (Result) {
                _this.fileList[_this.fileList.length - 1].pic = e.target.result
                if (Result.Status == "success") {
                  _this.fileList[_this.fileList.length - 1].Picture_Url = Result.Picture_Url
                  _this.$message.success('上传成功')
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //商品详情图片
    uploadDetails(file) {
      let _this = this
      this.fileList1.push({
        article_picNumber: '',
        Article_SerialNumber: '',
        Picture_Order: this.fileList1.length + 1,
        Picture_Type: '1',
        Picture_Url: '',
        pic: ''
      })
      let num = 1
      if (file.size > 150000 && file.size < 500000) {
        num = 0.4
      } else if (file.size > 500000) {
        num = 0.2
      }
      new Compressor(file, {
        quality: num,
        success(file) {
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                _this.fileList1[_this.fileList1.length - 1].pic = e.target.result
                if (Result.Status == "success") {
                  _this.$message.success('上传成功')
                  _this.fileList1[_this.fileList1.length - 1].Picture_Url = Result.Picture_Url
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //查看图片
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file
    },
    //删除图片
    handleRemove(type, index) {
      if (type === 'front') {
        this.form.pic = ''
      } else if (type === 'banner') {
        this.fileList = this.fileList.filter(function (item) {
          return item.Picture_Order != index
        })
      } else if (type === 'details') {
        this.fileList1 = this.fileList1.filter(function (item) {
          return item.Picture_Order != index
        })
      } else if (type === 'barcode') {
        this.fileList2 = this.fileList2.filter(function (item) {
          return item.Picture_Order != index
        })
      }
      else if (type === 'Specifications') {
        console.log(this.form.Children.article_specifications[0])
        this.form.Children.article_specifications[0].Picture_URL = ''
      }
    },
    beforeRemove(file, fileList) {
      this.fileList.pop()
    },
    uploadDetails1(file) {

    },
    submitForm1() {

    }
  }
})